package io.kiki.sba.registry.client.remoting;


import io.kiki.sba.registry.client.api.Configurator;
import io.kiki.sba.registry.client.model.ConfiguratorData;
import io.kiki.sba.registry.client.provider.ConfiguratorImpl;
import io.kiki.sba.registry.client.provider.RegisterCache;
import io.kiki.sba.registry.client.task.ObserverHandler;
import io.kiki.sba.registry.core.model.ReceivedConfigData;
import io.kiki.sba.registry.core.model.Result;
import io.kiki.stack.netty.processor.BizContext;
import io.kiki.stack.netty.userProcessor.SyncUserProcessor;
import lombok.Getter;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

@Setter
@Getter
public class ReceivedConfigDataProcessor extends SyncUserProcessor<ReceivedConfigData> {
    private static final Logger logger = LoggerFactory.getLogger(ReceivedConfigDataProcessor.class);

    private RegisterCache registerCache;

    private ObserverHandler observerHandler;

    public ReceivedConfigDataProcessor(RegisterCache registerCache, ObserverHandler observerHandler) {
        this.registerCache = registerCache;
        this.observerHandler = observerHandler;
    }


    @Override
    public Object syncHandleRequest(BizContext bizCtx, ReceivedConfigData request) {
        Result result = new Result();
        result.setSuccess(true);
        if (null == request) {
            return result;
        }

        List<String> registIds = request.getConfiguratorRegistIds();
        try {

            ConfiguratorData configuratorData = new ConfiguratorData();
            configuratorData.setData(request.getData());
            configuratorData.setVersion(request.getVersion());
            for (String registId : registIds) {
                Configurator configurator = registerCache.getRegistIdToConfiguratorMap().get(registId);
                if (null == configurator) {
                    continue;
                }

                if (configurator instanceof ConfiguratorImpl) {
                    ((ConfiguratorImpl) configurator).putConfiguratorData(configuratorData);
                    try {
                        observerHandler.notify(configurator);
                    } catch (Exception e) {
                        logger.error("[received] add configurator notify task error, dataId: {}, registId: {}", configurator.getDataId(), configurator.getRegistId(), e);
                    }
                } else {
                    logger.warn("[received] ignore unknown configurator type: {}", configurator.getClass().getName());
                }
            }
            logger.info("[received] receive configurator data save success, dataId: {} version: {} data:{} registIds:{}", request.getDataId(), request.getVersion(), request.getData(), registIds);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("");
            logger.info("[received] receive configurator data save failed, dataId: {} version: {} data:{}", request.getDataId(), request.getVersion(), request.getData(), e);
        }
        return result;
    }


    @Override
    public String interest() {
        return ReceivedConfigData.class.getName();
    }
}
